local voucherId = ARGS[1]
local userId = ARGS[2]

-- 库存的key
local stockKey = 'seckill:stock:' .. voucherId;
-- 订单key
local orderKey = 'seckill:order:' .. voucherId;

-- 1. 判断库存是否充足
local stockNum = tonumber(redis.call('get', stockKey), 10)
if stockNum < 1 then
    return 1
end

-- 2. 判断用户是否已经下过单
if redis.call('sismember', orderKey, userId) == 1 then
    return 2
end

-- 3. 扣减库存，并且把userId添加到优惠券set集合中
redis.call('decr', stockKey)
redis.call('sadd', orderKey, userId)
return 0